//
//  MineViewModel.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/6.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "MineViewModel.h"
#import "MineModel.h"
#import "BuyLotteryModel.h"
#import "WinRecordModel.h"
#import "CollcetionModel.h"

@interface MineViewModel()
@property (nonatomic,assign) NSInteger page;
@end

@implementation MineViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.showCoreArrayList = [[NSMutableArray alloc] initWithCapacity:0];
        self.userinfoModel = [[UserInfoModel alloc] init];
        self.userbuyLotteryList = [[NSMutableArray alloc] initWithCapacity:0];
        self.collectionInfoArrayList = [[NSMutableArray alloc] initWithCapacity:0];

    }
    return self;
}

//用户签到
-(RACSignal *)userSignRequestParameter{

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * param = @{
                                 @"userId": UserInfoShare.userCenterModel.userID,
                                 };
        
       [RequestOperationManager userSignParametersDic:param success:^(NSDictionary *result) {
           
           if ([result[@"errorcode"] integerValue] == 0) {
               
               UserInfoShare.userCenterModel.userIsSign = [NSString stringWithFormat:@"%@",result[@"result"][@"isSign"]];
           }

           [subscriber sendNext:result];
           [subscriber sendCompleted];
           
       } failture:^(id result) {
           
           [subscriber sendError:result];
       }];
        
        return nil;
    }];
}
//绑定真实信息
-(RACSignal *)bandRealInfoRequestParameterName:(NSString *)realName card:(NSString *)cardId{

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * param = @{
                                 @"phoneNumber":[DEF_UserDefaults objectForKey:DEF_USERDEFAULTS_USERNAME],
                                 @"realName"   :realName,
                                 @"idCard"     :cardId,
                                 @"userId"     :UserInfoShare.userCenterModel.userID,
                                 };
        [RequestOperationManager bandRealInfoParametersDic:param success:^(NSDictionary *result) {
            
            if ([result[@"errorcode"] integerValue] == 0) {
                                
                UserInfoShare.userCenterModel.userIsBinding = [NSString stringWithFormat:@"%@",result[@"result"][@"isBanding"]];
                UserInfoShare.userCenterModel.userRealName = result[@"result"][@"userRealName"];
                UserInfoShare.userCenterModel.userIdCard = result[@"result"][@"userIdCard"];
                
            }
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
        }];
        
        return nil;
    }];
}
//解除绑定
-(RACSignal *)cancelBandRealInfoRequestParameter{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
      
        
        NSDictionary * param = @{
                                 @"userId":UserInfoShare.userCenterModel.userID,
                                 };

        [RequestOperationManager cancelBandRealInfoParametersDic:param success:^(NSDictionary *result) {
            
            if (result) {
                
                if ([result[@"errorcode"] integerValue] == 0) {
                    
                    UserInfoShare.userCenterModel.userIsBinding = [NSString stringWithFormat:@"%@",result[@"result"][@"isBanding"]];
                    
                }
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
        } failture:^(id result) {
            [subscriber sendError:result];
        }];
        
        return nil;
    }];
}
//积分明细
-(RACSignal *) showCoreDetailRequestParameter{

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * param = @{
                                 @"userId"      :UserInfoShare.userCenterModel.userID,
                                 };
        [RequestOperationManager showCoreDetailParametersDic:param success:^(NSDictionary *result) {
            
            if (result) {
                
                self.userScore = [NSString stringWithFormat:@"%@",result[@"userScore"]];
                self.showCoreArrayList = (NSMutableArray *)[MTLJSONAdapter modelsOfClass:[ShowCoreModel class] fromJSONArray:result[@"result"] error:nil];
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [self.showCoreArrayList removeAllObjects];
            [subscriber sendError:result];
        }];
        return nil;
    }];
}

//个人中心
-(RACSignal *)userinfoDetailRequestParameterUserID:(NSString *)userid{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * parma = @{@"userId" : userid};
        
        [RequestOperationManager userinfoDetailParametersDic:parma success:^(NSDictionary *result) {
            
            if (result) {
                
                self.userinfoModel = [MTLJSONAdapter modelOfClass:[UserInfoModel class] fromJSONDictionary:result[@"result"] error:nil];
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
        }];
        
        return nil;
    }];
}
//购彩记录
-(RACSignal *) findUserBuyInfoRequestParameter:(NSString *) typeId{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * param = @{
                                 @"userId":UserInfoShare.userCenterModel.userID,
                                 @"typeId":[NSString stringWithFormat:@"%@",typeId]
                                 };
        
        [RequestOperationManager userbuyLotteryParametersDic:param success:^(NSDictionary *result) {
            
            if (result) {
                
    
                NSLog(@"%@",result);
                
                [self.userbuyLotteryList addObjectsFromArray:(NSMutableArray *)[MTLJSONAdapter modelsOfClass:[BuyLotteryModel class] fromJSONArray:result[@"result"] error:nil]];
                
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [self.userbuyLotteryList removeAllObjects];
            [subscriber sendError:result];
        }];
        return nil;
    }];
}

//上传头像
-(RACSignal *)uploadImageRequestParameter:(NSString *)file{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * parma = @{@"baseStr"    : file,
                                 @"userId"     :UserInfoShare.userCenterModel.userID};
        
        
        
        
        [RequestOperationManager upLoadImageParametersDic:parma success:^(NSDictionary *result) {
          
            if (result) {
                
                NSLog(@"result 上传头像 %@",result);
                UserInfoShare.userCenterModel.userImgPath = result[@"result"][@"imgPath"];
            }
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
        }];
        
        return nil;
    }];
}

//修改签名
-(RACSignal *) updateSignRequestParameter:(NSString *)userSign{

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * parma = @{@"userSign"    :userSign,
                                 @"userId"      :UserInfoShare.userCenterModel.userID};
        
        [RequestOperationManager updateUserInfoSignNamePwdParametersDic:parma success:^(NSDictionary *result) {
            
            if (result) {
                
                NSLog(@"修改签名 %@ %@",result,result[@"error"]);
                UserInfoShare.userCenterModel.userSign = result[@"result"][@"userSign"];
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
        }];
        return nil;
    }];
}
//修改昵称
-(RACSignal *)updateUserNameRequestParameter:(NSString *)userName{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * parma = @{@"username"    :userName,
                                 @"userId"      :UserInfoShare.userCenterModel.userID};
        
        [RequestOperationManager updateUserInfoSignNamePwdParametersDic:parma success:^(NSDictionary *result) {
            
            if (result) {
                
                UserInfoShare.userCenterModel.userName = result[@"result"][@"username"];
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
        }];
        return nil;
    }];
    
}
//修改密码
-(RACSignal *)updateUserPwdRequestParameter:(NSString *)userPwd{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * parma = @{@"oldPassword" :[DEF_UserDefaults objectForKey:DEF_USERDEFAULTS_USERPASSWORD],
                                 @"password"    :userPwd,
                                 @"userId"      :UserInfoShare.userCenterModel.userID};
        
        [RequestOperationManager updateUserInfoSignNamePwdParametersDic:parma success:^(NSDictionary *result) {
            
            if (result) {
                
                if ([result[@"errorcode"] integerValue] == 0) {
                    
                    UserInfoShare.isLogin = NO;
                    UserInfoShare.userCenterModel = nil;
                    
                    [DEF_UserDefaults removeObjectForKey:DEF_USERDEFAULTS_USERNAME];
                    [DEF_UserDefaults removeObjectForKey:DEF_USERDEFAULTS_USERPASSWORD];
                    [DEF_UserDefaults synchronize];
                    
                }
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
        }];
        return nil;
    }];
}

//彩票收藏
-(RACSignal *) collectionInfoRequestParameter:(LoadType)loadtype{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        if (loadtype == LoadData) {
            self.page = 0;
        }
        
        NSDictionary * param = @{
                                 @"userId"      :UserInfoShare.userCenterModel.userID,
                                 @"pageNo"      :[NSString stringWithFormat:@"%ld",(long)self.page],
                                 @"pageSize"    :DEF_PAGESIZE,
                                 };
        [RequestOperationManager collectionInfoParametersDic:param success:^(NSDictionary *result) {
            
            if (result) {
                
                if (loadtype == LoadData) {
                    
                    [self.collectionInfoArrayList removeAllObjects];
                }
                
                [self.collectionInfoArrayList addObjectsFromArray:(NSMutableArray *)[MTLJSONAdapter modelsOfClass:[CollcetionModel class] fromJSONArray:result[@"result"] error:nil]];
                
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
            
            self.page ++;
            
        } failture:^(id result) {
            
            [self.collectionInfoArrayList removeAllObjects];
            [subscriber sendError:result];
        }];
        return nil;
    }];
    
}
@end
